package com.yhy.wxzn.wcs.websocket.bean;

import com.yhy.wxzn.common.enums.WcsDeviceTypeEnums;
import com.yhy.wxzn.common.utils.SpringUtil;
import com.yhy.wxzn.wcs.device.plc.bean.HoistPlc;
import com.yhy.wxzn.wcs.device.plc.xj.XjPlcDriver;
import com.yhy.wxzn.wcs.entity.WcsDeviceInfo;
import com.yhy.wxzn.wcs.service.impl.WcsDeviceInfoServiceImpl;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.yhy.wxzn.wcs.device.plc.xj.XjPlcServer.plcDriverMap;

@Data
public class LiferInfo {

    //设备编号
    private Integer hoistNo;
    //设备名称
    private String hoistName;
    //就绪状态
    private String readyState;
    //提升机当前层
    private Integer curFloor;
    //任务编号
    private Integer taskNo;
    //设备位置
    private String deviceLocation;



    public static List<LiferInfo> buildLiferInfos(){
        ArrayList<LiferInfo> list = new ArrayList<>();
        WcsDeviceInfoServiceImpl deviceInfoService = SpringUtil.getBean(WcsDeviceInfoServiceImpl.class);
        List<WcsDeviceInfo> lifterInfos = deviceInfoService.findAllByDeviceType(WcsDeviceTypeEnums.ELV.getValue());
        for (WcsDeviceInfo deviceInfo : lifterInfos) {
            LiferInfo liferInfo = new LiferInfo();
            liferInfo.setHoistNo(deviceInfo.getId());
            liferInfo.setHoistName(deviceInfo.getDeviceName());
            XjPlcDriver xjPlcDriver = plcDriverMap.get(deviceInfo.getId());
            if (xjPlcDriver == null) continue;
            HoistPlc hoistPlc = xjPlcDriver.getDriverData().getHoistPlcMap().get(deviceInfo.getId());
            liferInfo.setReadyState(HoistPlc.parseWorkStatus(hoistPlc.getWorkStatus()));
            liferInfo.setCurFloor(hoistPlc.getFloor());
            liferInfo.setTaskNo(hoistPlc.getTaskNo());
            liferInfo.setDeviceLocation(null);
            list.add(liferInfo);
        }
        return list;
    }

    public static List<LiferInfo> buildTestLiferInfos(){
        ArrayList<LiferInfo> res = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            LiferInfo liferInfo = new LiferInfo();
            liferInfo.setTaskNo(111);
            liferInfo.setCurFloor(2);
            liferInfo.setDeviceLocation("中");
            liferInfo.setReadyState("就绪");
            liferInfo.setHoistName("提升机"+i);
            liferInfo.setHoistNo(i);
            res.add(liferInfo);
        }
        return res;
    }
}
